package org.googlecode.openapi_java;

import org.json.JSONException;
import org.json.JSONObject;

//TODO captcha error
public class VkontakteException extends Exception {

	// TODO full error list;
	public static final int ERROR_UNKNOWN = 1;
	public static final int ERROR_APPLICATION_DISABLED = 2;
	public static final int ERROR_UNKNOWN_METHOD_PASSED = 3;
	public static final int ERROR_INCORRECT_SIGNATURE = 4;
	public static final int ERROR_AUTH_FAIL = 5;
	public static final int ERROR_TOO_MANY_REQUESTS = 6;
	public static final int ERROR_PERMISSION_DENIED = 7;
	public static final int ERROR_CAPTCHA_NEEDED = 14;
	public static final int ERROR_PARAMETRS_MISSED = 100;

	public static final int ERROR_RESPONCE_CORRUPTED = -1024;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String message;
	private int code;

	public boolean isCaptchaException() {
		return (code == ERROR_CAPTCHA_NEEDED);
	}

	public int getCaptchaID() {
		// TODO
		return 0;
	}
	
	public String getCaptchaImage() {
		// TODO
		return null;
	}
	
	

	VkontakteException(JSONException exception) {
		message = exception.getMessage();
	}

	public VkontakteException(JSONObject errorMessage) {
		try {
			JSONObject error = errorMessage.getJSONObject("error");
			message = error.getString("error_msg");
			code = error.getInt("error_code");
		} catch (JSONException e) {
			message = e.getMessage();
		}
	}
	
	@Override
	public String getMessage() {
		return message;
	}

}
